package com.example.ssmxuankeback.controller;

import com.example.ssmxuankeback.common.RestBean;
import com.example.ssmxuankeback.dto.EnrollmentDTO;
import com.example.ssmxuankeback.dto.EnrollmentPageQueryDTO;
import com.example.ssmxuankeback.service.EnrollmentService;
import com.example.ssmxuankeback.vo.EnrollmentVO;
import com.example.ssmxuankeback.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Tag(name = "EnrollmentController", description = "选课管理接口")
@RequestMapping("/enrollments")
public class EnrollmentController {

    private final EnrollmentService enrollmentService;

    @Operation(summary = "创建选课记录")
    @PostMapping("/")
    public RestBean<EnrollmentVO> createEnrollment(
            @RequestBody @Validated EnrollmentDTO dto) {
        return enrollmentService.addEnrollment(dto);
    }

    @Operation(summary = "退选课程（更新状态为无效）")
    @DeleteMapping("/{enrollId}")
    public RestBean<EnrollmentVO> cancelEnrollment(
            @Parameter(description = "选课记录ID", required = true)
            @PathVariable Long enrollId) {
        return enrollmentService.cancelEnrollment(enrollId);
    }

    @Operation(summary = "分页查询选课记录")
    @GetMapping("/page")
    public RestBean<PageVO<EnrollmentVO>> getEnrollmentPage(
            @Validated EnrollmentPageQueryDTO queryDTO) {
        return enrollmentService.listByPage(queryDTO);
    }

    @Operation(summary = "根据学生ID查询选课记录")
    @GetMapping("/student/{studentId}")
    public RestBean<List<EnrollmentVO>> getEnrollmentsByStudentId(
            @Parameter(description = "学生ID", required = true)
            @PathVariable Long studentId) {
        return enrollmentService.getByStudentId(studentId);
    }

    @Operation(summary = "根据课程ID查询选课记录")
    @GetMapping("/course/{courseId}")
    public RestBean<List<EnrollmentVO>> getEnrollmentsByCourseId(
            @Parameter(description = "课程ID", required = true)
            @PathVariable Long courseId) {
        return enrollmentService.getByCourseId(courseId);
    }
}